package business.cdss;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;
import java.util.HashMap;

import static common.Login.login;

public class InsertSimilarRecord {

    Logger log = Logger.getLogger(InsertSimilarRecord.class);
    public static final String system = "env.properties";
    Data data = login();

    public HashMap insertSimilarRecord() throws ConfigurationException, FileNotFoundException, YamlException {

        //获取患者列表中第一个患者信息
        GetPatientList getPatientList = new GetPatientList();
        JSONArray contentArr = getPatientList.getlist();
        JSONObject jsonObject = (JSONObject) contentArr.get(0);
        String hospitalcode = jsonObject.getString("HOSPITAL_CODE");
        String patiid = jsonObject.getString("PATI_ID");
        String patientname = jsonObject.getString("PATIENTNAME");
        String patientcode = jsonObject.getString("INPATIENT_NO");
        //替换yaml上对应class的data值 把patiid，hospitalcode，name的值替换成患者信息的值
        String path = new GetFileMess().getValue("ymlPath", system);
        Yaml yaml = YamlReadUtil.getLocatorMap(path, this.getClass().getName());
        String a = yaml.getDatas().get(0).get("data").toString();
        String b = a.replace("patiid", patiid).replace("hospitalcode", hospitalcode).replace("name", patientname);

        //组合参数发出post请求
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("org_ids", "true");
        json.put("inpatientNo",patientcode);
        json.put("name", yaml.getDatas().get(0).get("name"));
        json.put("ptType", yaml.getDatas().get(0).get("ptType"));
        json.put("data", b);
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, yaml.getMethod(), yaml.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        JSONObject obj = JSON.parseObject(jsonStr);
        String patientcode1=obj.getString("rowData");

        HashMap hashMap=new HashMap();
        hashMap.put("patientcode",patientcode1);
        hashMap.put("hospitalcode",hospitalcode);
        return hashMap;
    }


    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {
        InsertSimilarRecord insertSimilarRecord=new InsertSimilarRecord();
        HashMap map=insertSimilarRecord.insertSimilarRecord();
        System.out.println(map.toString());

    }
}
